package com.unitlib.net.parser;

import com.unitlib.net.bean.PageList;
import com.unitlib.net.bean.Response;

import java.io.IOException;
import java.lang.reflect.Type;

import rxhttp.wrapper.annotation.Parser;
import rxhttp.wrapper.entity.ParameterizedTypeImpl;
import rxhttp.wrapper.exception.ParseException;
import rxhttp.wrapper.parse.AbstractParser;

@Parser(name = "ResponsePageList", wrappers = {PageList.class})
public class ResponsePageListParser<T> extends AbstractParser<PageList<T>> {

    protected ResponsePageListParser() {
        super();
    }

    public ResponsePageListParser(Type type) {
        super(type);
    }

    @Override
    public PageList<T> onParse(okhttp3.Response response) throws IOException {
        Type type = ParameterizedTypeImpl.get(Response.class, PageList.class, mType); //获取泛型类型
        Response<PageList<T>> data = convert(response, type);
        PageList<T> pageList = data.getData();
        if (!data.getReturn_code().equals("000000")) { //code不等于000000，说明数据不正确，抛出异常
            throw new ParseException(data.getReturn_code(), data.getReturn_message(), response);
        }
        return pageList;
    }
}
